package com.yq.cp.controller.yw;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yq.cp.entity.common.ApiResult;
import com.yq.cp.entity.yw.YwBjgl;
import com.yq.cp.entity.yw.dto.YwBjglPageDto;
import com.yq.cp.entity.yw.vo.YwBjglVo;
import com.yq.cp.entity.yw.vo.YwGdxxVo;
import com.yq.cp.service.yw.YwBjglService;
import com.yq.cp.utils.ExcelUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 * 运维-报警管理 前端控制器
 * </p>
 *
 * @author zh
 * @since 2024-09-07
 */
@RestController
@RequestMapping("/yw-bjgl")
public class YwBjglController {

    @Resource
    YwBjglService ywBjglService;

    @ResponseBody
    @PostMapping("page")
    public ApiResult page(@RequestBody YwBjglVo vo) {
        IPage<YwBjgl> page = ywBjglService.page(vo);
        return ApiResult.ok().data(page);
    }

    @ResponseBody
    @PostMapping("page/daochu")
    public ApiResult pageDaochu(@RequestBody YwBjglVo vo) {
        List<YwBjglPageDto> daochu = ywBjglService.daochu(vo);
        ExcelUtil.doWrite("报警管理","报警管理",YwBjglPageDto.class,daochu);
        return ApiResult.ok();
    }

    @ResponseBody
    @PostMapping("sc")
    public ApiResult sc(@RequestBody YwBjgl ywBjgl) {
        ywBjglService.sc(ywBjgl.getId());
        return ApiResult.ok();
    }

    @ResponseBody
    @PostMapping("get/daochu")
    public ApiResult getDaochu(@RequestBody YwGdxxVo vo, HttpServletResponse response, HttpServletRequest request) throws UnsupportedEncodingException {
        YwBjgl ywBjgl = ywBjglService.get(vo.getId());
        // 防止日志记录获取session异常
        request.getSession();
        // 设置编码格式
        response.setContentType("application/pdf;charset=UTF-8");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("报警详情", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".pdf");
        ywBjglService.getDaochu(ywBjgl, response);
        return ApiResult.ok();
    }
}

